var gulp = require("gulp");
var gulp_min_css = require("gulp-minify-css");
var gulp_sass = require("gulp-sass");
var gulp_plumber = require("gulp-plumber");
var gulp_tinypng = require("gulp-tinypng");
var gulp_uglify = require("gulp-uglify");
var browser_sync = require("browser-sync").create();
var gulp_copy = require("gulp-copy");

//自动刷新浏览器
gulp.task('bsync', function() {
    browser_sync.init({
        server: {
            baseDir: "src/"   //根地址
        }
    });
    gulp.watch("src/sass/**/*.scss",['sass']).on("change", browser_sync.reload);
    gulp.watch("src/*.html").on("change", browser_sync.reload);
});
//
gulp.task('sass',function(){
	return gulp.src('src/sass/**/*.scss')
			   .pipe(gulp_sass().on('error',gulp_sass.logError))
			   .pipe(gulp.dest('src/css'));
})

//定义任务
gulp.task("mincss",function(){
	//具体事情
	//管道模式
	gulp.src('src/sass/**/*.scss')
		.pipe(gulp_plumber())
		.pipe(gulp_sass())
		.pipe(gulp_min_css())
		.pipe(gulp.dest('src/dist/css'))
})

//实时监听sass文件，并转化
gulp.task("watchscss",function(){
	gulp.watch('src/sass/**/*.scss',["mincss"])
})
//图片压缩
gulp.task("minimg",function(){
	gulp.src('src/img/**/*.*')
		.pipe(gulp_plumber())
		.pipe(gulp_tinypng("8xUS5CwNCUPDIqvpWg6eIbvJGI05a6h0"))
		.pipe(gulp.dest('src/dist/img'))
})
//js压缩
gulp.task("minjs",function(){
	guli.src("src/js/**/*.js")
		.pipe(gulp_uglify())
		.pipe(gulp.dest('src/dist/js'))
})
//复制文件
gulp.task("copy",function(){
	gulp.src('src/*.html').pipe(gulp_copy('./dist'));
	gulp.src('src/img/**/*').pipe(gulp_copy('./dist'));
	gulp.src('src/css/**/*.css').pipe(gulp_copy('./dist'));
	gulp.src('src/js/**/*.js').pipe(gulp_copy('./dist'));
	gulp.src('src/sass/**/*.scss').pipe(gulp_copy('./dist'));
})

//定义一键开发模式
gulp.task("dev",["bsync"]);

//定义一键生成模式
gulp.task("pro",["mincss","minjs","minimg"]);
